<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类型" prop="type">
      <!-- <el-input v-model="dataForm.type" placeholder="类型(1 课程风采，2 作品展示 ，3 文化考级 , 4 各项赛事)"></el-input> -->
      
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.disName"
            :value="item.value">
          </el-option>
        </el-select>
      
    </el-form-item>
    <el-form-item label="图片" prop="imgs">
      <el-upload
        class="upload-demo"
        multiple
        :action="url"
        :on-success="handleAvatarSuccess"
        :before-remove="handleChange"
        :file-list="dataForm.fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="序号" prop="cadetNo">
      <el-input v-model="dataForm.cadetNo" placeholder="序号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        url: '',
        fileListUrls: [],
        typeList:[{
          disName: '课程风采',
          value: 1
        },{
          disName: '作品展示',
          value: 2
        },{
          disName: '文化考级',
          value: 3
        },{
          disName: '各项赛事',
          value: 4
        }],
        visible: false,
        dataForm: {
          cadetStyleId: 0,
          type: '',
          imgs: '',
          cadetNo: '',
          fileList: []
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          fileList: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          cadetNo: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.fileListUrls=[]
        this.dataForm.fileList=[]
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.cadetStyleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.cadetStyleId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/cadesstyle/info/${this.dataForm.cadetStyleId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              //debugger;
              if (data && data.code === 0) {
                this.dataForm.type = data.cadesStyle.type
                this.dataForm.cadetNo = data.cadesStyle.cadetNo
                // this.fileListUrls =JSON.parse(data.cadesStyle.imgs)
                debugger;
                let arr = JSON.parse(data.cadesStyle.imgs)
                this.fileListUrls=arr
                arr.forEach(element => {
                  this.dataForm.fileList.push({name:element, url:element});
                });
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/cadesstyle/${!this.dataForm.cadetStyleId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'cadetStyleId': this.dataForm.cadetStyleId || undefined,
                'type': this.dataForm.type,
                'imgs': JSON.stringify(this.fileListUrls),
                'cadetNo': this.dataForm.cadetNo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
       handleChange (file, fileList) {
        this.fileListUrls.splice(fileList.indexOf(file),1);
      },
       handleAvatarSuccess(res, file,fileList) {
         debugger;
        if (res && res.code === 0) {
          this.fileListUrls.push(res.url)
          this.$message.success('上传成功')
        } else {
          this.$message.error(res.msg)
        }
        console.log(this.fileListUrls)
      },
    }
  }
</script>

<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>